package com.bolt.support.ui.tree.helper;

import com.bolt.support.ui.tree.model.TreeDataProvider;
import com.bolt.support.ui.tree.vo.UITreeNode;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by michao on 2017/10/31.
 */
public class TreeRenderHelper {


    public static void buildNodesTreeView(TreeDataProvider provider,
                                                   Serializable parentId, Map <String, String> params,List<UITreeNode> dataArray,
                                                   boolean loadAllNodes) {


        List <UITreeNode> childs = provider.getChildren(parentId, params);
        for (UITreeNode child : childs) {
            if (loadAllNodes) {
                child.setOpen(true);
            }

            if (loadAllNodes && child.hasChildren()) {
                List <UITreeNode> childrenArray = new ArrayList <>();
                buildNodesTreeView(provider, child.getId(), params, childrenArray, loadAllNodes);
                if (childrenArray.size() > 0) {
                    child.setChildren(childrenArray);
                }
            }
            dataArray.add(child);
        }

    }

}


